'use strict';


// 用户相关请求响应定义
const adminUserReqAndResp = require('../entitys/reqAndResp/admin/adminUser');
// 用户角色相关请求响应定义
const adminRolesReqAndResp = require('../entitys/reqAndResp/admin/adminRoles');
// 宠物品类相关相应定义
const petcateReqAndResp = require('../entitys/reqAndResp/admin/petcate');
// 犬类和犬主人相应定义
const petregistermaster = require('../entitys/reqAndResp/admin/petmasterregister');
// 菜单管理
const sysMenusReqAndResp = require('../entitys/reqAndResp/admin/sysMenus');
// 旧系统相关请求
const oldPetInfoReqAndResp = require('../entitys/reqAndResp/admin/oldPetInfo');
// 犬证库存管理
const dogNumStockReqAndResp = require('../entitys/reqAndResp/admin/dogNumStock');
// 操作文件类库
const fs = require('fs');

/**
 * 挂在 扩展方法到 request对象上
 */
module.exports = {
  // 获取带图片的请求参数
  async getMultipart() {
    const parts = this.ctx.multipart();
    const uploaderId = this.ctx.curAdminUser.id;
    let part = {};
    const modelInfo = {};
    while ((part = await parts()) != null) {
      if (part.length) {
        modelInfo[`${part[0]}`] = part[1];
        const regExp = /^data:[img,image]+\/\w+;base64,/;
        if (part[1].match(regExp)) {
          const base64 = part[1].replace(regExp, ''); // 去掉图片base64码前面部分data:image/png;base64
          const dataBuffer = new Buffer(base64, 'base64'); // 把base64码转成buffer对象
          const imgPath = `${process.cwd()}/app/public/images/${uploaderId}_${part[0]}_${new Date().getTime()}.jpg`;
          modelInfo[`${part[0]}`] = imgPath;
          fs.writeFile(imgPath, dataBuffer, function (err) { // 用fs写入文件
            if (err) {
              console.log(err);
            } else {
              console.log('写入成功！');
            }
          });
        }
      } else {
        if (!part.filename) continue;
        modelInfo[`${part.fieldname}`] = '';
        if (part.fieldname) {
          const imgPath = `${process.cwd()}/app/public/images/${uploaderId}_${part.fieldname}_${new Date().getTime()}.jpg`;
          modelInfo[`${part.fieldname}`] = imgPath;
          const fileStream = fs.createWriteStream(imgPath);
          await part.pipe(fileStream);
        }
      }
    }
    this.body = Object.assign(modelInfo, this.body);
  },

  // 登录
  getAndValidate_LoginRequest() {
    let request = adminUserReqAndResp.login.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 查询用户列表
  getAndValidate_QueryAdminUsersList() {
    let request = adminUserReqAndResp.queryAdminUsersList.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 用户详情
  getAndValidate_QueryAdminUser() {
    let request = adminUserReqAndResp.queryAdminUser.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 添加用户
  getAndValidate_AddUserRequest() {
    let request = adminUserReqAndResp.addAdminUser.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 删除用户
  getAndValidate_DeleteAdminUserRequest() {
    let request = adminUserReqAndResp.deleteAdminUser.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 更新用户
  getAndValidate_UpdateAdminUserRequest() {
    let request = adminUserReqAndResp.updateAdminUser.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 查询所有角色列表（分页）
  getAndValidate_QueryAdminRolesList() {
    let request = adminRolesReqAndResp.queryAdminRolesList.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 添加角色
  getAndValidate_addAdminRoleRequest() {
    let request = adminRolesReqAndResp.addAdminRoles.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 修改角色
  getAndValidate_UpdateAdminRoleRequest() {
    let request = adminRolesReqAndResp.updateAdminRoles.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 删除角色(逻辑删除)
  getAndValidate_DeleteAdminRoleRequest() {
    let request = adminRolesReqAndResp.DeleteAdminRoles.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 设置角色权限
  getAndValidate_SetRoleAuthoRequest() {
    let request = adminRolesReqAndResp.SetRoleAutho.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 获取角色权限
  getAndValidate_GetRoleAuthoRequest() {
    let request = adminRolesReqAndResp.getRoleAutho.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 获取角色拥有的菜单列表（功能级别）
  getAndValidate_getRoleMenuUserRights() {
    let request = adminRolesReqAndResp.getRoleMenuUserRights.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },

  // 添加菜单
  getAndValidate_addSysMenus() {
    let request = sysMenusReqAndResp.addSysMenu.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  },
  // 更新菜单
  getAndValidate_updateMenu() {
    let request = sysMenusReqAndResp.updateMenu.request();
    this.ctx.validate(request);
    request = this.body;
    return request;
  }
};